use std::ffi::OsStr;
use std::path::Path;
use std::io::Result;

pub fn all_file_path() {
    // 获取当前路径
    let path = std::env::current_dir().unwrap();
    println!("{}", path.display());

    match get_files("../demodata/", &["rs", "toml"]) {
        Ok(files) => {
            for file in files {
                println!("{}", file);
            }
        }
        Err(e) => eprintln!("文件读取失败: {}", e),
    }
}

// 获取路径以及嵌套路径下的所有文件
fn get_files<P: AsRef<Path>>(path: P, extensions: &[&str]) -> Result<Vec<String>> {
    let mut files = Vec::new();

    for entry in std::fs::read_dir(path)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() {
            let matches = match path.extension() {
                Some(ext) => extensions
                    .iter()
                    .any(|e| OsStr::new(e).eq_ignore_ascii_case(ext)),
                None => false,
            };
            if matches || extensions.is_empty() {
                let path_str = path.display().to_string()
                    .replace('\\', "/"); // 统一转为斜杠
                files.push(path_str);
            }
        } else if path.is_dir() {
            files.extend(get_files(path, extensions)?);
        }
    }
    Ok(files)
}
